﻿using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Mt.Site.Core.Authorization.Roles;
using Mt.Site.Core.Base.Authorization;
using Mt.Site.Core.Base.Authorization.Roles;
using Mt.Site.Core.Base.Authorization.Users;
using Mt.Site.EntityFrameworkCore;
using System.Linq;

namespace Mt.Site.Migrations.Seed.Tenants
{
    public class TenantRoleAndUserBuilder
    {
        private readonly MtSiteDbContext _context;
        private readonly int _tenantId;

        public TenantRoleAndUserBuilder(MtSiteDbContext context, int tenantId)
        {
            _context = context;
            _tenantId = tenantId;
        }

        public void Create()
        {
            CreateRolesAndUsers();
        }

        private void CreateRolesAndUsers()
        {
            //User role
            var userRole = _context.Roles.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.User);
            if (userRole == null)
            {
                _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.User, StaticRoleNames.Tenants.User) { IsStatic = true, IsDefault = true });
                _context.SaveChanges();
            }

            var defaultCultureName = DefaultTenantLanguagesCreator.InitialLanguages.FirstOrDefault()?.Name;
            //一个系统管理员
            var systemUser = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == _tenantId && u.UserName == User.SystemUserName);
            if (systemUser == null)
            {
                systemUser = UserFunction.CreateSystemUser(_tenantId, "system@defaulttenant.com");
                systemUser.Password = new PasswordHasher<User>(new OptionsWrapper<PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(systemUser, "bnm970");
                systemUser.IsEmailConfirmed = true;
                systemUser.ShouldChangePasswordOnNextLogin = false;
                systemUser.IsActive = true;
                systemUser.CultureName = defaultCultureName;
                _context.Users.Add(systemUser);
                _context.SaveChanges();

                ////User account of system user
                //_context.UserAccounts.Add(new UserAccount
                //{
                //    TenantId = _tenantId,
                //    UserId = systemUser.Id,
                //    UserName = systemUser.Name,
                //    EmailAddress = systemUser.EmailAddress
                //});
                //_context.SaveChanges();
            }

            //一个超级管理员
            var adminUser = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == _tenantId && u.UserName == User.AdminUserName);
            if (adminUser == null)
            {
                adminUser = UserFunction.CreateAdminUser(_tenantId, "admin@defaulttenant.com");
                adminUser.Password = new PasswordHasher<User>(new OptionsWrapper<PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(adminUser, "123qwe");
                adminUser.IsEmailConfirmed = true;
                adminUser.IsActive = true;
                adminUser.CultureName = defaultCultureName;
                adminUser.ShouldChangePasswordOnNextLogin = false;
                _context.Users.Add(adminUser);
                _context.SaveChanges();

                ////User account of admin user
                //_context.UserAccounts.Add(new UserAccount
                //{
                //    TenantId = _tenantId,
                //    UserId = adminUser.Id,
                //    UserName = adminUser.Name,
                //    EmailAddress = adminUser.EmailAddress
                //});
                //_context.SaveChanges();
            }

            var mountorUserForHost = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == _tenantId && u.UserName == User.SuperUserName);
            if (mountorUserForHost == null)
            {
                mountorUserForHost = UserFunction.CreateMountorUser(_tenantId, "mountor@defaulttenant.com");
                mountorUserForHost.Password = new PasswordHasher<User>(new OptionsWrapper<PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(mountorUserForHost, "bnm970");
                mountorUserForHost.IsEmailConfirmed = true;
                mountorUserForHost.ShouldChangePasswordOnNextLogin = false;
                mountorUserForHost.IsActive = true;
                _context.Users.Add(mountorUserForHost);
                _context.SaveChanges();
            }
        }
    }
}
